package pathfinding;

import java.io.Serializable;

/**
 * Gridcell goes from (x * GridGeometry.gridSize, y * GridGeometry.gridSize) 
 *                 -> ((x+1) * GridGeometry.gridSize, (y+1) * GridGeometry.gridSize)   
 * 
 * @author gilead
 *
 */
public class GridCell implements Serializable {
	
	private static final long serialVersionUID = 1L;
	int x, y;
	public GridCell(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	public GridCell clone()
	{
		return new GridCell(x, y);
	}
	
	/**
	 * Required to be able to function as key in hashmap.
	 */
	public boolean equals(Object another)
	{
		if (another == null) {
			return false;
		}
		
		return (another instanceof GridCell && 
				((GridCell)another).x == x &&
				((GridCell)another).y == y);
	}
	
	/**
	 * If this.equals(another) is true, hashCode(this) == hashCode(another) should also be true.
	 * Other way around is not required
	 * 
	 * Required to be able to function as key in hashmap.
	 * 
	 * This one is optimized to work for cells from <-100,-100> to <100, 100>
	 */
	public int hashCode()
	{
		return (x + 100) * 200 + y;
		/*
		int hash = 17;
		hash = hash * 23 + x;
		hash = hash * 23 + y;
		return hash;
		*/
		
	}
	
	public String toString()
	{
		return "[" + x + ", " + y + "]";
	}
	
	public GridCell north()
	{
		return new GridCell(x, y+1);
	}
	
	public GridCell northeast()
	{
		return new GridCell(x+1, y+1);
	}
	
	public GridCell east()
	{
		return new GridCell(x+1, y);
	}
	
	public GridCell southeast()
	{
		return new GridCell(x+1, y-1);
	}
	
	public GridCell south()
	{
		return new GridCell(x, y-1);
	}
	
	public GridCell southwest()
	{
		return new GridCell(x-1, y-1);
	}
	
	public GridCell west()
	{
		return new GridCell(x-1, y);
	}
	
	public GridCell northwest()
	{
		return new GridCell(x-1, y+1);
	}
}
